package com.arsn.myf.quartz.controller;

import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.quartz.config.ScheduleManager;
import com.arsn.myf.quartz.entity.ScheduledJob;
import com.arsn.myf.quartz.service.ScheduledJobService;
import com.arsn.myf.quartz.vo.ScheduledJobVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 定时任务 
 * @author zhongrj
 * @since 2022-03-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/quartz/scheduledJob")
@Api(value = "定时任务", tags = "定时任务")
public class ScheduledJobController {
    private final ScheduledJobService scheduledJobService;

    private final ScheduleManager scheduleManager;

    /**
     * 查询定时任务的分页数据
     * @param page
     * @param scheduledJob
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询定时任务的分页数据")
    public R list(Page<ScheduledJob> page, ScheduledJob scheduledJob){
        return R.data(scheduledJobService.page(page,new QueryWrapper<>(scheduledJob)));
    }


    /**
     * 自定义查询定时任务的分页数据
     * @param page
     * @param scheduledJob
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询定时任务的分页数据")
    public R page(Page<ScheduledJobVO> page, ScheduledJobVO scheduledJob){
        return R.data(scheduledJobService.selectPageScheduledJobList(page,scheduledJob));
    }

    /**
     * 定时任务详情信息
     * @param scheduledJob
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "定时任务详情信息")
    public R detail(ScheduledJob scheduledJob){
        return R.data(scheduledJobService.getOne(new QueryWrapper<>(scheduledJob)));
    }


    /**
     * 定时任务新增
     * @param scheduledJob
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "定时任务新增")
    public R save(@RequestBody ScheduledJob scheduledJob){
        scheduledJob.setCreateTime(new Date());
        boolean status = scheduledJobService.save(scheduledJob);
        if (status){
            //创建定时任务
            scheduleManager.createScheduledJob(scheduledJob);
        }
        return R.status(status);
    }

    /**
     * 定时任务修改
     * @param scheduledJob
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "定时任务修改")
    public R update(@RequestBody ScheduledJob scheduledJob){
        scheduledJob.setUpdateTime(new Date());
        boolean status = scheduledJobService.updateById(scheduledJob);
        if (status){
            //修改定时任务
            scheduleManager.updateScheduledJob(scheduledJob);
        }
        return R.status(status);
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除,批量删除")
    public R remove(@NotNull String ids){
        List<String> list = Arrays.asList(ids.split(","));
        list.forEach(s -> {
            ScheduledJob scheduledJob = new ScheduledJob();
            scheduledJob.setId(Integer.parseInt(s));
            //删除定时任务
            scheduleManager.deleteScheduledJob(scheduledJob);
        });
        return R.status(scheduledJobService.removeByIds(Func.stringToLong(list)));
    }

    /**
     * 定时任务恢复
     * @param scheduledJob
     * @return
     */
    @PostMapping("/activateQuartz")
    @ApiOperation(value = "定时任务恢复")
    public R activateQuartz(@RequestBody ScheduledJob scheduledJob){
        scheduledJob.setUpdateTime(new Date());
        boolean status = scheduledJobService.updateById(scheduledJob);
        if (status){
            //修改定时任务
            scheduleManager.resumeJob(scheduledJob);
        }
        return R.status(status);
    }

    /**
     * 立即执行任务
     * @param scheduledJob
     * @return
     */
    @PostMapping("/runQuartz")
    @ApiOperation(value = "立即执行任务")
    public R runQuartz(@RequestBody ScheduledJob scheduledJob){
        scheduledJob.setUpdateTime(new Date());
        boolean status = scheduledJobService.updateById(scheduledJob);
        if (status){
            //立即启动定时任务
            scheduleManager.run(scheduledJob);
        }
        return R.status(status);
    }

    /**
     * 暂停任务
     * @param scheduledJob
     * @return
     */
    @PostMapping("/stopQuartz")
    @ApiOperation(value = "暂停任务")
    public R stopQuartz(@RequestBody ScheduledJob scheduledJob){
        scheduledJob.setUpdateTime(new Date());
        boolean status = scheduledJobService.updateById(scheduledJob);
        if (status){
            //暂停定时任务
            scheduleManager.pauseJob(scheduledJob);
        }
        return R.status(status);
    }
}
